
/**
 * 公共方法js
 * 
 */

function getType(key) {
	return Cache.get(key, 'TypesCache');
}

function getTypeLabel(key, value) {
	var arr = Cache.get(key, 'TypesCache');
	for (var i in arr) {
		if (arr[i].code === parseInt(value)) {
			return arr[i].label;
		}
	}
	return stateLabel.unState;
}

function getList(key, callback) {
	return Cache.get(key, 'ListCache', callback);
}

function getListLabel(key, value) {
	var arr = Cache.get(key, 'ListCache');
	for (var i in arr) {
		if (arr[i].code == value) {
			return arr[i].label;
		}
	}
	return listLabel.unList;
}

function getLabelByArray(arr, value) {
	for (var i in arr) {
		if (arr[i].code == value) {
			return arr[i].label;
		}
	}
	return listLabel.unList;
}

function getFunAuthority(key) {
	return Cache.get(key, 'FunAuthorityCache');
}

function addTab(opt) {
	top.vm && top.vm.openTag(opt);
}

function getUrlParam(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return (false);
}

function message(context, type) {
	var vm = vm || top.vm;
	if (!vm) {
		alert(context);
		return;
	}
	vm.$message({
		message : context,
		type : (type || 'error'),
		showClose : true
	});
}

//---------------------------------------------------    
//日期格式化    
//格式 YYYY/yyyy/YY/yy 表示年份    
//MM/M 月份    
//W/w 星期    
//dd/DD/d/D 日期    
//hh/HH/h/H 时间    
//mm/m 分钟    
//ss/SS/s/S 秒    
//---------------------------------------------------    
Date.prototype.Format = function(formatStr) {
	var str = formatStr || 'yyyy-MM-dd HH:mm:ss';
	var Week = [ '日', '一', '二', '三', '四', '五', '六' ];

	str = str.replace(/yyyy|YYYY/, this.getFullYear());
	str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));

	str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? ((this.getMonth() + 1) + '') : '0' + (this.getMonth() + 1));
	str = str.replace(/M/g, (this.getMonth() + 1));

	str = str.replace(/w|W/g, Week[this.getDay()]);

	str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
	str = str.replace(/d|D/g, this.getDate());

	str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
	str = str.replace(/h|H/g, this.getHours());
	str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
	str = str.replace(/m/g, this.getMinutes());

	str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
	str = str.replace(/s|S/g, this.getSeconds());

	return str;
}

function moneyFormat(value){
    if (value == null || value == 0 || value == undefined || value == "&#160;") {//如果是空
        return '0.00';
    }else{
        var result = (value.toString()).indexOf(".");
        if(result != -1) {
            //带小数点
            if (value.toString().substring(result,value.length).length >= 3) {//如果小数点到末尾的长度大于等于3
                if (typeof value == 'string') {//因为toFixed只对Number起作用，所以，如果当前的值是string类型，则转为number
                    return parseInt(value).toFixed(2)+'';  
                }
                return value.toFixed(2)+''; //截取小数点后两位
            }else{
                return value+'0';
            }
            return value+'';
        } else {
            //不带小数点
            return value+'.00';
        }
        return value+'';
    }
}

String.prototype.firstUpperCase=function(){
    return this.replace(/^\S/,function(s){return s.toLowerCase();});
}


function crefresh( key ) {
	return Cache.get(key, 'ListCache', function ( data ){
		vm[key + 's'] = data;
	} , true);
}


function crefreshType( key ) {
	return Cache.get(key, 'TypesCache', function ( data ){
		vm[key + 's'] = data;
	} , true);
}

function loginDialog( ) {
	top.vm.$message({
        dangerouslyUseHTMLString: true,
        message: '<strong>请重新登录</strong>'
  });
}

function clearForm( form ) {
	for (var i in form) {
		form[i] = null;
	}
}

function tranformToHump(str){
    var strArr=str.split('-');
    for(var i=0;i<strArr.length;i++){
        strArr[i]=strArr[i].charAt(0).toUpperCase()+strArr[i].substring(1);
    }
    return strArr.join('');
}

function getQueryString(name) { 
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
    var r = window.location.search.substr(1).match(reg); 
    if (r != null) return unescape(r[2]); 
    return null; 
} 

function validatePage( length ){
	if (!vm) {return true;}
	try {
		var size = vm.pageBarPageSize;
		var page = vm.pageBarCurrentPage;
		var maxPage = length%size == 0 ? (length/size) : (length/size + 1);
		if (page > maxPage) {
			vm.pageBarCurrentPage = parseInt(maxPage);
			vm.toolBarSearch();
			return false;
		}
		return true;
	} catch(e) {}
}